#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <Includes.h>
#include <DRV_Neul.h>
#include <DRV_Neul_AT.h>
#include <OPP_Debug.h>

extern neul_dev_info_t ec600s_dev;
neul_dev_info_t *dev[EN_DEV_TYPE_MAX]={&ec600s_dev};

int AtResponseComm(char *buffer, int len, unsigned char *exdata)
{
    if(NULL != strstr(buffer,"OK"))
        return 0;
    return 1;
}

/**
@brief AT指令执行函数
@param tpye 设备类型
@param cmd AT指令
@param timeout 超时时间
@param atRspProc 响应处理函数
@param odata 返回数据
@return 0.成功,-1.写失败,-2读失败(命令执行超时), -4解析异常,<-100解异常s
**/
int AtExe(EN_DEV_TYPE type,char *cmd, int timeout, int (*atRspProc)(char *buffer, int len, unsigned char *exdata), ST_AT_EXE_OUT_DATA *odata)
{
    int ret;

    DEBUG_LOG(DEBUG_MODULE_NEUL, DLL_INFO,"cmd=%s\r\n", cmd);

    //ret = dev->ops->dev_write(cmd, STRLEN(cmd), 0);
    ret = dev[type]->ops->dev_write(cmd, strlen(cmd), 0);
    if (ret < 0)
    {
        DEBUG_LOG(DEBUG_MODULE_NEUL, DLL_ERROR,"%s write err=%d\r\n", cmd, ret);
        return -1;
    }
    memset(dev[type]->rbuf, 0, NEUL_MAX_BUF_SIZE);
    ret = dev[type]->ops->dev_read(dev[type]->rbuf, NEUL_MAX_BUF_SIZE, 0, timeout);
    if (ret <= 0)
    {
        DEBUG_LOG(DEBUG_MODULE_NEUL, DLL_ERROR,"%s read err=%d\r\n", cmd, ret);
        return -2;
    }
    DEBUG_LOG(DEBUG_MODULE_NEUL, DLL_INFO,"read=%s\r\n", dev[type]->rbuf);

    if(odata!=NULL && odata->raw_data!=NULL)
    {
        strncpy(odata->raw_data,dev[type]->rbuf,ret);
    }

    if(NULL!=atRspProc)
    {
        ret = atRspProc(dev[type]->rbuf,ret,(odata==NULL?NULL:odata->extract_data));
        if(0!=ret)
        {
            return ret-100;
        }
    }
    else
    {
        ret=AtResponseComm(dev[type]->rbuf, ret, (odata==NULL?NULL:odata->extract_data));
        if(0!=ret)
        {
            return -4;
        }
    }

    return 0;
}

/*
*-1没找到
*/
int AtCmdFind(ST_CMD *tbl, int num, char *cmd)
{
    int i=0;

    for(i=0;i<num;i++)
    {
        if(0==strcmp(tbl[i].atCmd,cmd))
            return i;
    }
    return -1;
}

/*执行单条AT指令*/
int AtCmdOne(EN_DEV_TYPE type,ST_CMD *cmd, ST_AT_EXE_OUT_DATA *odata)
{
    return AtExe(type, cmd->atCmd, cmd->waitTime, cmd->atRspProc, odata);
}

int AtFmtCmdOne(EN_DEV_TYPE type,ST_CMD *cmd, ST_AT_EXE_OUT_DATA *odata, int len, ...)
{
	va_list vl;
    int ret;
    char *buf=(char *)malloc(len);
    if(buf==NULL)
    {
        return 2;
    }

    va_start(vl,len);
    vsprintf(buf,cmd->atCmd,vl);
    va_end(vl);

    ret=AtExe(type, buf, cmd->waitTime, cmd->atRspProc, odata);
    free(buf);
    return ret;
}

/*执行序列AT指令*/
int AtCmd(EN_DEV_TYPE type, ST_CMD cmd[],int num)
{
    int i,idx;
    int ret;
    //int n=1;

    for(i=0;i<num;)
    {
        /*CMD_START(AtExe(cmd[i].atCmd, cmd[i].waitTime, cmd[i].atRspProc, NULL),n,ret)
        {
			DEBUG_LOG(DEBUG_MODULE_EC600S, DLL_ERROR, "%s ret %d\r\n", cmd[i].atCmd,ret);
        }
        CMD_END();*/

        ret=AtExe(type, cmd[i].atCmd, cmd[i].waitTime, cmd[i].atRspProc, NULL);
        //执行正确且有指定nextcmd,执行下一个cmd
        if(ret==0 && NULL!=cmd[i].nextCmd)
        {
            idx=AtCmdFind(cmd, num, cmd[i].nextCmd);
            if(-1!=idx)
                i=idx;
            else
                i++;

        }
        else
        {
            i++;
        }
    }
    return 0;
}


